/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.task;

import akka.actor.ActorRef;
import com.cxhd.dc.DataConfig;
import com.cxhd.dc.actor.ActorManager;
import com.cxhd.dc.tool.CxhdTran;
import com.cxhd.dc.tool.FileUtil;
import com.cxhd.dc.tool.HttpClientUt;
import com.cxhd.dc.tran.DownloadCallback;
import it.sauronsoftware.ftp4j.FTPClient;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author Administrator
 */
public class DownloadFileTask extends BaseTask {

    private BlockingQueue unzipQueue;
    private String result;
    FTPClient ftpClient;
    private ActorManager actorManager = ActorManager.getIntance();

    public DownloadFileTask(BlockingQueue unzipQueue) {
        this.unzipQueue = unzipQueue;
        // 获取没有被解压的文件
        List<File> list = FileUtil.getFile(DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getDownloadFolder(), true, ".f");
        if (list != null && !list.isEmpty()) {
            for (File file : list) {
                try {
                    System.out.println(file.getName());
                    //unzipQueue.put(file);
                     actorManager.getActor("upzipActor").tell(file, ActorRef.noSender());
                } catch (Exception ex) {

                }
            }
        }

    }

    @Override
    public BaseTask start() {
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("sn", DataConfig.getInstance().getLocalhostSN()));
            result = HttpClientUt.httpPost("http://" + DataConfig.getInstance().getCheckHost() + "/AWCenter/getFileList.jsp", params);
            System.out.println(result + "---------------" + "http://" + DataConfig.getInstance().getCheckHost() + "/AWCenter/getFileList.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }
        return this;
    }

    @Override
    public BaseTask doTask() {
        try {
            if (result != null && result.trim().equalsIgnoreCase("candown")) {
                try {
                    ftpClient = CxhdTran.getInstance().getClient();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                CxhdTran.getInstance().downloadFolder(ftpClient, DataConfig.getInstance().getLocalhostSN() + "/inbox", DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getDownloadFolder(), new DownloadCallback() {
                    @Override
                    public void finish(File file) {
                        actorManager.getActor("upzipActor").tell(file, ActorRef.noSender());
                    }
                });

            }
        } catch (Exception e) {
        }
        return this;
    }

    @Override
    public BaseTask stop() {
        if (ftpClient != null) {
            CxhdTran.getInstance().logout(ftpClient);
        }
        return this;
    }

    @Override
    public BaseTask cancelTask() {
        if (ftpClient != null) {
            CxhdTran.getInstance().logout(ftpClient);
        }
        return this;
    }

}
